package com.autocode.codegenerate.generate.impl;

import com.autocode.codegenerate.database.DbReadTableUtil;
import com.autocode.codegenerate.generate.IGenerate;
import com.autocode.codegenerate.generate.pojo.ColumnVo;
import com.autocode.codegenerate.generate.pojo.TableVo;
import com.autocode.codegenerate.generate.util.NonceUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @author qiujunda
 * @date 2019年5月2日
 * @description
 */
public class CodeGenerateOne extends com.autocode.codegenerate.generate.impl.ofile.OutPutFile implements IGenerate {
    private static final Logger logger = LoggerFactory.getLogger(CodeGenerateOne.class);
    private TableVo b;

    public CodeGenerateOne(TableVo tableVo) {
        this.b = tableVo;
    }

    public Map<String, Object> a() {
        new ArrayList();
        new ArrayList();
        HashMap var3 = new HashMap();
        var3.put("bussiPackage", com.autocode.codegenerate.init.InitConfig.h);
        var3.put("entityPackage", this.b.getEntityPackage());
        var3.put("entityName", this.b.getEntityName());
        var3.put("tableName", this.b.getTableName());
        var3.put("primaryKeyField", com.autocode.codegenerate.init.InitConfig.m);
        this.b.setFieldRequiredNum(StringUtils.isNotEmpty(com.autocode.codegenerate.init.InitConfig.n)?Integer.parseInt(com.autocode.codegenerate.init.InitConfig.n):-1);
        this.b.setSearchFieldNum(StringUtils.isNotEmpty(com.autocode.codegenerate.init.InitConfig.o)?Integer.parseInt(com.autocode.codegenerate.init.InitConfig.o):-1);
        var3.put("tableVo", this.b);

        try {
            List var1 = DbReadTableUtil.a(this.b.getTableName());
            var3.put("columns", var1);
            List var2 = DbReadTableUtil.b(this.b.getTableName());
            var3.put("originalColumns", var2);
            Iterator var4 = var2.iterator();

            while(var4.hasNext()) {
                ColumnVo var5 = (ColumnVo)var4.next();
                if(var5.getFieldName().toLowerCase().equals(com.autocode.codegenerate.init.InitConfig.m.toLowerCase())) {
                    var3.put("primaryKeyPolicy", var5.getFieldType());
                }
            }
        } catch (Exception var6) {
            var6.printStackTrace();
            System.exit(-1);
        }

        long var7 = NonceUtils.c() + NonceUtils.g();
        var3.put("serialVersionUID", String.valueOf(var7));
        logger.info("code template data: " + var3.toString());
        return var3;
    }

    public void generateCodeFile() {
        logger.info("----futurism---Code----Generation----[单表模型:" + this.b.getTableName() + "]------- 生成中。。。");
        String var1 = com.autocode.codegenerate.init.InitConfig.g;
        Map var2 = this.a();
        String var3 = com.autocode.codegenerate.init.InitConfig.k;
        if(a(var3, "/").equals("code_generate/template")) {
            var3 = "/" + a(var3, "/") + "/one";
        }

        com.autocode.codegenerate.generate.gfile.GenerateFile var4 = new com.autocode.codegenerate.generate.gfile.GenerateFile(var3);

        try {
            this.a(var4, var1, var2);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        logger.info("----futurism----Code----Generation-----[单表模型：" + this.b.getTableName() + "]------ 生成完成。。。");
    }

    public static void main(String[] args) {
        System.out.println("----futurism--------- Code------------- Generation -----[单表模型]------- 生成中。。。");
        TableVo var1 = new TableVo();
        var1.setTableName("demo");
        var1.setPrimaryKeyPolicy("uuid");
        var1.setEntityPackage("test");
        var1.setEntityName("futurismDemo");
        var1.setFtlDescription("futurism 测试demo");
        (new CodeGenerateOne(var1)).generateCodeFile();
        System.out.println("----futurism--------- Code------------- Generation -----[单表模型]------- 生成完成。。。");
    }
}
